import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  Toast
} from 'vant'
// import userRoutes from './modules/user'
// import testComponentRoutes from './modules/testComponent'

Vue.use(VueRouter)
const routes = [{
  path: '/',
  redirect: '/home'
}, {
  path: '/home',
  name: 'home',
  component: () => import(/* webpackChunkName:'home'*/ '@/views/home/Home.vue'),
  meta: {
    title: '首页',
    firefly_title: '总行_食堂_首页'
  }
}, {
  path: '/enterpriseList',
  name: 'enterpriseList',
  component: () => import(/* webpackChunkName:'home'*/ '@/views/enterpriseList/index.vue'),
  meta: {
    title: '智慧食堂列表',
    firefly_title: '总行_食堂_列表页'

  }
}, {
  path: '/citySelect',
  name: 'citySelect',
  component: () => import(/* webpackChunkName:'home'*/ '@/views/citySelect/index.vue'),
  meta: {
    title: '城市选择',
    firefly_title: '总行_食堂_城市选择'
  }
},
// ...userRoutes,
// ...testComponentRoutes,
{
  path: '/404',
  component: () => import('@/views/errorPage/404.vue')
}
]

const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch((err) => err)
}

router.beforeEach((to, from, next) => {
  // 萤火埋点
  // let title=from.meta.firefly_title
  if (window.sensors && from.meta.firefly_title) {
    window.sensors.registerPage({
      '$referrer_title': from.meta.firefly_title
    })
  }
  if (to.matched.length === 0) {
    next('/404')
  } else {
    next()
  }
})
router.afterEach((to, from) => {
  // 萤火埋点
  if (window.sensors && to.meta.firefly_title) {
    window.sensors.quick('autoTrackSinglePage', {
      '$title': to.meta.firefly_title
    })
  }
})
export default router
